﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Net;
using System.Collections.Specialized;

namespace ThereforeSystems.Samples.Google.UrlShortener
{
    /// <summary>
    /// Shortener class is used to access Google Shortener API.
    /// This class provides two methods:
    /// <list type="number">
    /// <item>
    ///     <term>GetShortUrl()</term>
    ///     <description>Gets a reply with short Url available in id property.</description>
    /// </item>
    /// <item>
    ///     <term>GetLongUrl()</term>
    ///     <description>Gets a reply with long Url available in longUrl property.</description>
    /// </item>
    /// </list>
    /// </summary>
   public class Shortener
    {
       private string googleShortenerUrl = "https://www.googleapis.com/urlshortener/v1/url";
       private string apiKey;

       public Shortener() { }

       /// <summary>
       /// Initializes a new instance of the <see cref="Shortener"/> class.
       /// </summary>
       /// <param name="apiKey">When a non-blank apiKey is passed, it
       /// will be used for all subsequent calls for this instance.</param>
       public Shortener(string apiKey)
       {
           if (!string.IsNullOrWhiteSpace(apiKey))
           {
               googleShortenerUrl += "?key="+ apiKey;
           }
       }

       /// <summary>
       /// Gets the short Url by calling Google Shortener API.
       /// </summary>
       /// <param name="longUrl">The long URL.</param>
       /// <returns>An instance of <see cref="Reply"/> class. 
       /// Short Url can be found in id property of Reply class.
       /// </returns>
       public Reply GetShortUrl(string longUrl)
       {
           string data = "{\"longUrl\":\"" + longUrl + "\"}";
           string postUrl = googleShortenerUrl;
           string response = HttpHelper.HttpPOST(postUrl, data);
           return DeserializeJSON<Reply>(response);
       }

       /// <summary>
       /// Gets the long Url by calling Google Shortener API.
       /// </summary>
       /// <param name="shortUrl">The short URL.</param>
       /// <returns>An instance of <see cref="Reply"/> class. 
       /// Short Url can be found in longUrl property of Reply class.
       /// </returns>
       public Reply GetLongUrl(string shortUrl)
       {
           string getUrl = googleShortenerUrl + "&shortUrl=" + shortUrl;
           string response = HttpHelper.HttpGET(getUrl);
           return DeserializeJSON<Reply>(response);
       }

       /// <summary>
       /// Deserializes the JSON recieved from Google.
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="input">JSON recieved from Google.</param>
       /// <returns>An instance of type T</returns>
       private static T DeserializeJSON<T>(string input)
       {
           using (MemoryStream memoryStream = 
               new MemoryStream(Encoding.UTF8.GetBytes(input)))
           {
               DataContractJsonSerializer serializer =
                   new DataContractJsonSerializer(typeof(T));
               
               T reply;

               reply = (T)serializer.ReadObject(memoryStream);
               memoryStream.Close();

               return reply;
           }
       }
    }

   
}
